#include "game.h"
#include "ui_game.h"

game::game(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::game),blsRun(false)
{
    ui->setupUi(this);
    this->setGeometry(QRect(100,100,1000,800));
}

game::~game()
{
    delete ui;
}

void game::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    if(!blsRun)
    {
        InitCat();
    }

    painter.setPen(Qt::blue);
    painter.setBrush(Qt::gray);
    painter.drawRect(10,10,980,780);

    painter.setPen(Qt::green);
    painter.setBrush(Qt::green);
    painter.drawRect(rock_1);
    painter.drawRect(rock_2);
    painter.drawRect(rock_3);

    painter.setPen(Qt::yellow);
    painter.setBrush(Qt::yellow);
    painter.drawRects(&vCatRect[0],vCatRect.size());


    QFont font_1("Courier",90);
    painter.setFont(font_1);
    painter.setPen(Qt::red);
    painter.setBrush(Qt::red);
    painter.drawText(450,400,sDisplay);

    if(blsOver)
    {
        timer->stop();
    }
}

void game::InitCat()
{
    nDirection=0;
    blsRun=true;
    blsOver=false;
    rock_1=CreateRock_1();
    rock_2=CreateRock_2();
    rock_3=CreateRock_3();
    vCatRect.resize(6);
    for(int i=0;i<vCatRect.size();i++)
    {
        QRect rect(50+40*i,390,40,40);
        vCatRect[vCatRect.size()-1-i]=rect;
    }
    timer=new QTimer(this);
    timer->start(10);
    connect(timer,SIGNAL(timeout()),SLOT(Cat_update()));
    connect(timer,SIGNAL(timeout()),SLOT(rock_1_update()));
    connect(timer,SIGNAL(timeout()),SLOT(rock_2_update()));
    connect(timer,SIGNAL(timeout()),SLOT(rock_3_update()));
}

void game::keyPressEvent(QKeyEvent *event)
{
    QKeyEvent *key=(QKeyEvent*)event;
    switch(key->key())
    {
    case Qt::Key_W:nDirection=1;
        break;
    case Qt::Key_D:nDirection=2;
        break;
    case Qt::Key_S:nDirection=3;
        break;
    case Qt::Key_A:nDirection=4;
        break;
    case Qt::Key_Shift:Cat_step=2;
        break;
    case Qt::Key_K:nDirection=0;
        break;
    default:;
    }
}

void game::keyReleaseEvent(QKeyEvent *event)
{
    QKeyEvent *key=(QKeyEvent *)event;
    if(key->key()==Qt::Key_Shift)
    {
        Cat_step=1;
    }
}

void game::Cat_update()
{
    IsHit_rock_1();
    IsHit_rock_2();
    IsHit_rock_3();
    CatHead=vCatRect.first();
    for(int j=0;j<vCatRect.size()-1;j++)
    {
        vCatRect[vCatRect.size()-1-j]=vCatRect[vCatRect.size()-2-j];
    }
    switch(nDirection)
    {
    case 0:
        break;
    case 1:
        if((CatHead.top()>=11&&Cat_step==1)||(CatHead.top()>=12&&Cat_step==2))
        {
            CatHead.setTop(CatHead.top()-Cat_step);
            CatHead.setBottom(CatHead.bottom()-Cat_step);
        }
        break;
    case 2:
        if((CatHead.right()<=989&&Cat_step==1)||(CatHead.right()<=988&&Cat_step==2))
        {
            CatHead.setRight(CatHead.right()+Cat_step);
            CatHead.setLeft(CatHead.left()+Cat_step);
        }
        break;
    case 3:
        if((CatHead.bottom()<=789&&Cat_step==1)||(CatHead.bottom()<=788&&Cat_step==2))
        {
         CatHead.setTop(CatHead.top()+Cat_step);
         CatHead.setBottom(CatHead.bottom()+Cat_step);
        }
        break;
    case 4:
        if((CatHead.left()>=11&&Cat_step==1)||(CatHead.left()>=12&&Cat_step==2))
        {
            CatHead.setRight(CatHead.right()-Cat_step);
            CatHead.setLeft(CatHead.left()-Cat_step);
        }
        break;
    default:;
    }
    vCatRect[0]=CatHead;
    update();
}

void game::rock_1_update()
{
    if(rock_1.left()<=10)
    {
        int x,y;
        x=qrand()%50;
        y=qrand()%740;
        QRect rect(880+x,20+y,40,40);
        rock_1=rect;
    }
    else
    {
        rock_1.setLeft(rock_1.left()-1);
        rock_1.setRight(rock_1.right()-1);
    }
    update();
}

void game::rock_2_update()
{
    if(rock_2.left()<=10)
    {
        int x,y;
        x=qrand()%50;
        y=qrand()%740;
        QRect rect(880+x,20+y,40,40);
        rock_2=rect;
    }
    else
    {
        rock_2.setLeft(rock_2.left()-1);
        rock_2.setRight(rock_2.right()-1);
    }
    update();
}

void game::rock_3_update()
{
    if(rock_3.left()<=10)
    {
        int x,y;
        x=qrand()%50;
        y=qrand()%740;
        QRect rect(880+x,20+y,40,40);
        rock_3=rect;
    }
    else
    {
        rock_3.setLeft(rock_3.left()-1);
        rock_3.setRight(rock_3.right()-1);
    }
    update();
}

QRect game::CreateRock_1()
{
    int x,y;
    x=qrand()%50;
    y=qrand()%740;
    QRect rect(880+x,20+y,40,40);
    return rect;
}

QRect game::CreateRock_2()
{
    int x,y;
    x=qrand()%50;
    y=qrand()%740;
    QRect rect(880+x,20+y,40,40);
    return rect;
}

QRect game::CreateRock_3()
{
    int x,y;
    x=qrand()%50;
    y=qrand()%740;
    QRect rect(880+x,20+y,40,40);
    return rect;
}

void game::IsHit_rock_1()
{
    for(int i=1;i<vCatRect.size();i++)
    {
        if(vCatRect[i].top()>=rock_1.top()&&vCatRect[i].top()<=rock_1.bottom()&&vCatRect[i].right()>=rock_1.left()&&vCatRect[i].right()<=rock_1.right())
        {
            sDisplay="喵喵被石头绊倒了！";
            blsOver=true;
            update();
        }
    }
}

void game::IsHit_rock_2()
{
    for(int i=1;i<vCatRect.size();i++)
    {
        if(vCatRect[i].top()>=rock_2.top()&&vCatRect[i].top()<=rock_2.bottom()&&vCatRect[i].right()>=rock_2.left()&&vCatRect[i].right()<=rock_2.right())
        {
            sDisplay="喵喵被石头绊倒了！";
            blsOver=true;
            update();
        }
    }
}

void game::IsHit_rock_3()
{
    for(int i=1;i<vCatRect.size();i++)
    {
        if(vCatRect[i].top()>=rock_3.top()&&vCatRect[i].top()<=rock_3.bottom()&&vCatRect[i].right()>=rock_3.left()&&vCatRect[i].right()<=rock_3.right())
        {
            sDisplay="喵喵被石头绊倒了！";
            blsOver=true;
            update();
        }
    }
}
